package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Comment;
import com.itheima.service.CommentService;
import com.itheima.utils.MessageConstant;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/comment")
@CrossOrigin
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     * @param commentVO
     * @return
     */
    @PostMapping("/add")
    public String add(@RequestBody CommentVO commentVO) {
        try {
            commentService.add(commentVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_UNAUTHORIZED,MessageConstant.COMMENT_ADD_FAIL);
        }
        return ResultUtil.result(HttpServletResponse.SC_UNAUTHORIZED,MessageConstant.COMMENT_ADD_SUCCESS);
    }

    /**
     * 删除评论
     *@param commentVO
     * @return com.itheima.entity.Result
     *@author itheima
     */
    @PostMapping("/delete")
    public String delete(@RequestBody CommentVO commentVO) {
        try {
            commentService.delete(commentVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_UNAUTHORIZED,MessageConstant.COMMENT_DELETE_FAIL);
        }
        return ResultUtil.result( HttpServletResponse.SC_OK,MessageConstant.CATEGORYMENU_DELETE_SUCCESS);
    }

    /**
     * 删除选中评论
     *@param commentVOList
     * @return com.itheima.entity.Result
     *@author itheima
     */
    @PostMapping("/deleteBatch")
    public String deleteBatch(@RequestBody List<CommentVO> commentVOList) {
        try {
            commentService.deleteBatch(commentVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_UNAUTHORIZED,MessageConstant.COMMENT_DELETE_FAIL);
        }
        return ResultUtil.result(HttpServletResponse.SC_OK,MessageConstant.COMMENT_DELETE_SUCCESS);
    }

    /**
     * 编辑评论
     *@param commentVO
     * @return com.itheima.entity.Result
     *@author itheima
     */
    @PostMapping("/edit")
    public String edit(@RequestBody CommentVO commentVO) {
        try {
            commentService.edit(commentVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_OK,MessageConstant.CATEGORYMENU_EDIT_FAIL);
        }
        return ResultUtil.result(HttpServletResponse.SC_OK,MessageConstant.CATEGORYMENU_EDIT_SUCCESS);
    }

    /**
     * 获取评论列表
     *@param commentVO
     * @return com.itheima.entity.Result
     *@author itheima
     */
    @PostMapping("/getList")
    public String getList(@RequestBody CommentVO commentVO) {
        List<Comment> commentList = commentService.getList(commentVO);
        return ResultUtil.result(HttpServletResponse.SC_OK,commentList);
    }
}
